package service

import (
	"database/sql"
	"log"
)

///添加部门
func (db DB) AddDept(reqMap map[string]interface{}) bool {
	//从json中提取参数
	name := reqMap["name"].(string)
	parentId := int(reqMap["parentId"].(float64))
	var houseNo interface{}
	if reqMap["house_no"] != nil {
		houseNo = reqMap["house_no"].(string)
	}
	var tel interface{}
	if reqMap["tel"] != nil {
		tel = reqMap["tel"].(string)
	}

	//SQL预处理
	sqlstr := "INSERT INTO dept(name,parent_id,house_no,tel)VALUES(?,?,?,?)"
	stmt, err := db.Conn.Prepare(sqlstr)
	if err != nil {
		log.Println("SQL Prepare error: ", err)
		return false
	}
	//SQL执行
	_, err = stmt.Exec(name, parentId, houseNo, tel)
	if err != nil {
		log.Println("SQL Exec error: ", err)
		return false
	}

	//返回
	return true
}

//修改部门信息
func (db DB) EditDept(reqMap map[string]interface{}) bool {
	//从json中提取参数
	id := int(reqMap["id"].(float64))
	name := reqMap["name"].(string)
	parentId := int(reqMap["parentId"].(float64))
	houseNo := reqMap["house_no"].(string)
	tel := reqMap["tel"].(string)

	//SQL预处理
	sql := "UPDATE dept SET name=?,parent_id=?,house_no=?,tel=? WHERE id=?"
	stmt, err := db.Conn.Prepare(sql)
	if err != nil {
		log.Println("SQL Prepare error: ", err)
		return false
	}
	//SQL执行
	_, err = stmt.Exec(name, parentId, houseNo, tel, id)
	if err != nil {
		log.Println("SQL Exec error: ", err)
		return false
	}

	//返回
	return true
}

//删除部门
func (db DB) DelDept(reqMap map[string]interface{}) bool {
	//从json中提取参数
	id := int(reqMap["id"].(float64))

	//SQL预处理
	sql := "DELETE FROM dept WHERE id=?"
	stmt, err := db.Conn.Prepare(sql)
	if err != nil {
		log.Println("SQL Prepare error: ", err)
		return false
	}
	//SQL执行
	_, err = stmt.Exec(id)
	if err != nil {
		log.Println("SQL Exec error: ", err)
		return false
	}

	//返回
	return true
}

//获得部门信息
func (db DB) GetDept(reqMap map[string]interface{}) map[string]interface{} {
	//从json中提取参数
	queryId := int(reqMap["id"].(float64))

	//查询SQL
	sqlstr := "SELECT * FROM dept WHERE id=?"
	rows, err := db.Conn.Query(sqlstr, queryId)
	if err != nil {
		log.Println("Query error: ", err)
		rspEmpty := make(map[string]interface{})
		return rspEmpty
	}

	defer rows.Close() //方法返回时rows将关闭

	//查询结果集
	var (
		id       int
		name     string
		parentId int
		houseNo  sql.NullString
		tel      sql.NullString
	)
	if !rows.Next() {
		log.Println("Can't get the dept, id=", id)
		rspEmpty := make(map[string]interface{})
		return rspEmpty
	}
	if err = rows.Scan(&id, &name, &parentId, &houseNo, &tel); err != nil {
		log.Println("rows scan err: ", err)
		rspEmpty := make(map[string]interface{})
		return rspEmpty
	}

	//返回
	rsp := make(map[string]interface{})
	rsp["id"] = id
	rsp["name"] = name
	rsp["parentId"] = parentId
	if houseNo.Valid {
		rsp["houseNo"] = houseNo.String
	} else {
		rsp["houseNo"] = nil
	}
	if tel.Valid {
		rsp["tel"] = tel.String
	} else {
		rsp["tel"] = nil
	}

	return rsp
}

//获取组织树
func (db DB) Deptree(_nil interface{}) map[string]interface{} {
	//递归获取组织树
	list := deptree(db, 0)

	//返回包装为map
	m := make(map[string]interface{})
	m["id"] = 0
	m["name"] = "组织树"
	m["sub"] = list
	return m
}

//递归获取组织树
func deptree(db DB, queryParentId int) []map[string]interface{} {
	//返回集合
	var list []map[string]interface{}

	//查询SQL
	sql := "SELECT * FROM dept WHERE parent_id=?"
	rows, err := db.Conn.Query(sql, queryParentId)
	if err != nil {
		log.Println("Query error: ", err)
		return nil
	}

	defer rows.Close() //方法返回时rows将关闭

	//查询结果集
	var (
		id       int
		name     string
		parentId int
		houseNo  string
		tel      string
	)
	for rows.Next() {
		if err = rows.Scan(&id, &name, &parentId, &houseNo, &tel); err != nil {
			log.Println("rows scan err: ", err)
			break
		}

		m := make(map[string]interface{})
		m["id"] = id
		m["name"] = name
		m["parentId"] = parentId
		m["houseNo"] = houseNo
		m["tel"] = tel
		m["sub"] = deptree(db, id) //递归获得下级组织树
		list = append(list, m)     //当前部门map置入list
	}

	//返回
	return list
}
